package com.alks.function.mapper;

import com.alks.entity.data.entity.*;
import com.alks.entity.data.entity.dispatch.VenType;
import com.alks.entity.data.entity.dispatch.Vendor;
import com.alks.function.data.dto.*;
import com.alks.function.data.dto.chengKong.assort.SdCustTypeDto;
import com.alks.function.data.dto.chengKong.assort.SdMtlTypeNewDto;
import com.alks.function.data.request.MaterialLotRequest;
import com.alks.function.data.request.MaterialOutRequest;
import com.alks.function.data.request.MaterialRequest;
import com.alks.function.data.request.last.OutsoleOrderDetailReq;
import com.alks.function.data.request.operate.material.GeneralMaterialRequest;
import com.alks.function.data.request.operate.material.MaterialDetail1Request;
import com.alks.function.data.request.order.PcOrderMtlRegularSingleRequest;
import com.alks.function.data.request.stock.*;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.mapstruct.Mapper;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Mapper
public interface MaterialMapper {

    /**
     * 入库明细查询
     * @param request
     */
    List<MaterialDto> getInventoryDetails(MaterialRequest request);

    /**
     * 入库明细统计数量
     * @param request
     * @return
     */
    MaterialCountDto getInventoryDetailsList(MaterialRequest request);

    /**
     * 指令明细
     * @param request
     */
    List<MaterialLotDto> getLotDetailList(MaterialLotRequest request);

    /**
     * 指令明细统计数量
     * @param request
     * @return
     */
    MaterialLotCountDto getLotDetailListCount(MaterialLotRequest request);

    /**
     * 材料出库领料明细
     * @param request
     */
    List<MaterialOutDto> getMaterialDetailOutList(MaterialOutRequest request);

    /**
     * 材料出库实发明细
     * @param request
     * @return
     */
    List<MaterialSendOutDto> getSendList(MaterialLotRequest request);

    /**
     * 统计材料出库实发量
     * @param request
     * @return
     */
    BigDecimal getSendCount(MaterialLotRequest request);

    List<PcOrderMtlRegularSingleRep> pcOrderMtlRS(PcOrderMtlRegularSingleRequest request);

    List<PcOrderMtlRegularSingleRep> pcOrderMtlSo(PcOrderMtlRegularSingleRequest request);

    void pcOrderMtlMakeOrder(@Param("list") List<PcOrderMtlRegularSingleRep> list,@Param("username") String sysUser);


    @Select("SELECT SIZE_NO FROM PC_ORDER_HEAD WHERE ORDER_ID = #{lotNo}")
    String getSizeNo(@Param("lotNo") String lotNo);

    void pcOrderMtlMakeOrderPre(@Param("list") List<PcOrderMtlRegularSingleRep> list,@Param("username") String sysUser);

    void pcOrderMtlMakeOrderPurDetail(@Param("list") List<PcOrderMtlRegularSingleRep> list,@Param("username") String sysUser);

    void pcOrderMtlMakeOrderPrePurDetail(@Param("list") List<PcOrderMtlRegularSingleRep> list,@Param("username") String sysUser);

    List<PcOrderMaterialOrderDetailRep> pcOrderMtlMakeOrderDetail(@Param("request") PcOrderMaterialOrderDetailRequest request,@Param("username") String username);

    BigDecimal pcOrderMtlMakeOrderDetailQtyTotal(@Param("request") PcOrderMaterialOrderDetailRequest request,@Param("username") String username);

    BigDecimal pcOrderMtlMakeOrderDetailPurQtyTotal(@Param("request") PcOrderMaterialOrderDetailRequest request,@Param("username") String username);

    Integer pcOrderMtlMakeOrderDetailTotal(@Param("request") PcOrderMaterialOrderDetailRequest request,@Param("username") String username);

    void createPcOrderMtlOrder(@Param("request") PcOrderMtlOrderCreateRequest request,@Param("purNo") String purNo,@Param("username") String username);

    int setNullPcOrderMtlOrder(@Param("request") PcOrderMtlOrderCreateRequest request,@Param("username") String username);

    int updatePcOrderMtlOrder(@Param("request") PcOrderMaterialOrderDetailRep request,@Param("username") String userName);

    @Select("SELECT DISTINCT  DUTY_USER FROM PC_MTL_PRE_OTHER WHERE DUTY_USER IS NOT NULl")
    List<String> getDutyUsers();

    @Select("SELECT DISTINCT  DUTY_REK FROM PC_MTL_PRE_OTHER WHERE DUTY_REK IS NOT NULl")
    List<String> getDutyRek();

    void insertSdMtlNewKf(@Param("mtlTypeNo") String mtlTypeNo, @Param("rep") PcOrderPickingOperateDetailRep rep, @Param("username") String username);

    int updatePickingOperate1( PcOrderPickingOperateDetailRep req1 );
    @Select("SELECT MAX(CAST(RIGHT(MTL_NO,7) AS DECIMAL(10,2)))  FROM SD_MTL_NEW_KF WHERE SUBSTR(MTL_NO,1,4) = #{mtlTypeNo}")
    BigDecimal getMtlTypeNo2(String mtlTypeNo);

    int deleteByList1(@Param("list") List<PcOrderPickingOperateDetailRep> list,@Param("lotNo") String lotNo);

    int insertMaterial1(@Param("rep") PcOrderPickingOperateDetailRep rep,@Param("lotNo") String lotNo,@Param("username") String username);

    List<PcOrderMaterToAllRep> getMaterialADetail1(@Param("request") MaterialDetail1Request request,@Param("username") String userName);

    Integer getMaterialADetail1Total(@Param("request") MaterialDetail1Request request,@Param("username") String userName);

    int generalMaterial(@Param("list") PcOrderMaterToAllRep rep,@Param("username") String username);


    int setMtlNoEmptyPickingOperate(@Param("list") List<PcOrderMaterToAllRep> list,@Param("username") String username);

    void createPcOrderMtlOrder2(@Param("purNo") String purNo,@Param("request") GeneralMaterialRequest request,@Param("username") String username);

    void setNullPcOrderMtlOrder2(@Param("request") GeneralMaterialRequest request,@Param("username") String username);

    void deleteByList2(@Param("request") GeneralMaterialRequest request,@Param("username") String username);

    void insertMaterial2(@Param("rep") PcOrderMaterToAllRep rep,@Param("username") String username);

    void updateMaterial2(@Param("rep") PcOrderMaterToAllRep rep,@Param("username") String username);

    void updateMaterial3(@Param("rep") PcOrderMaterToAllRep rep,@Param("mtlNo") String mtlNo,@Param("username") String username);

    void updateMaterial4(@Param("newRep") PcOrderMaterToAllRep newRep,@Param("oldRep") PcOrderMaterToAllRep oldRep,@Param("username") String username);

    List<PcMtlMoldLotRep> getPcMtlMoldLot1(@Param("request") PcMtlMoldLotRequest request);

    List<PcMtlMoldLotRep> getPcMtlMoldLot2(@Param("request") PcMtlMoldLotRequest request);

    void insertIntoMtlPre1(@Param("request") CreateMtlMoldRequest request,@Param("username") String username);

    void insertIntoMtlPre2(@Param("request") CreateMtlMoldRequest request,@Param("username") String username);

    List<OutsoleOrderDetailRep> getOutsoleOrderDetail(@Param("request")OutsoleOrderDetailReq request,@Param("username") String username);

    BigDecimal getOutsoleOrderDetailQtyTotal(@Param("request")OutsoleOrderDetailReq request,@Param("username") String username);

    BigDecimal getOutsoleOrderDetailPurQtyTotal(@Param("request")OutsoleOrderDetailReq request,@Param("username") String username);

    int updateIntoMtlPre3(@Param("request") IMaterialIMReq request,@Param("username") String username);

    void setMtlNoInOther(@Param("rep") OutsoleOrderDetailRep rep,@Param("username") String username);

    void insertSdMtlNewKf3(@Param("rep") OutsoleOrderDetailRep rep,@Param("username") String username);

    void createSolePurNo(@Param("purNo") String purNo,@Param("request") PcSoleOperateReq request,@Param("username") String username);

    void deleteFromOther(@Param("list") List<OutsoleOrderDetailRep> list,@Param("username") String username);

    void insertMaterial3(List<OutsoleOrderDetailRep> list);

    void updateSolePur(@Param("rep1") OutsoleOrderDetailRep rep1,@Param("rep2") OutsoleOrderDetailRep rep2,@Param("username") String username);

    @Select("SELECT COLOR_SEQ COLOR_CODE,COLOR_SYSTEM COLOR_NAME FROM SD_COLOR_HEAD")
    List<PcMtlHueDesignRep> mtlHueDesign();

    @Select("SELECT COLOR_CODE,COLOR_NAME FROM SD_COLOR_DETAIL WHERE COLOR_SYSTEM = #{colorName}")
    List<PcMtlHueDesignRep> mtlColorDesign(String colorName);

    void insertHueDesign(@Param("rep") PcMtlHueDesignRep rep, @Param("name") String name);

    void insertColorDesign(@Param("rep") PcMtlHueDesignRep rep, @Param("name") String name, @Param("colorName") String colorName);

    void updateHueDesign(@Param("rep") PcMtlHueDesignRep rep, @Param("name") String name);

    void updateColorDesign(@Param("rep") PcMtlHueDesignRep rep, @Param("name") String name, @Param("colorName") String colorName);

    void deleteHueDesign(PcMtlHueDesignRep rep);

    void deleteColorDesign(@Param("rep") PcMtlHueDesignRep rep, @Param("colorName") String colorName);

    @Select("SELECT TYPE_NO,TYPE_NAME,LEVEL_CODE,PARENT_LEVEL,REMARK,MTL_PROPERTY,SIZE_FLAG FROM SD_MTL_TYPE_NEW")
    List<SdMtlTypeNewDto> getAllMtlProperty();

    @Select("SELECT MAX(LEVEL_CODE) FROM SD_MTL_TYPE_NEW WHERE PARENT_LEVEL = #{levelCode}")
    String getMaxByCurrentLevel(String levelCode);

    @Insert("INSERT INTO SD_MTL_TYPE_NEW(TYPE_NO,PARENT_LEVEL,LEVEL_CODE,TYPE_NAME) VALUES(#{typeNo},#{parentLevel},#{levelCode},#{typeName})")
    void insertSdMtlTypeNew1(@Param("parentLevel") String parentLevel, @Param("typeNo") String typeNo, @Param("typeName") String typeName, @Param("levelCode") String levelCode);

    @Delete("DELETE FROM SD_MTL_TYPE_NEW WHERE TYPE_NO = #{typeNo} AND LEVEL_CODE = #{levelCode}")
    void deleteSdMtlTypeNew(SdMtlTypeNewDto req);

    @Update("UPDATE SD_MTL_TYPE_NEW SET REMARK = #{remark},MTL_PROPERTY = NULLIF(#{mtlProperty},'') WHERE TYPE_NO = #{typeNo} AND LEVEL_CODE = #{levelCode}")
    void updateSdMtlTypeNew(SdMtlTypeNewDto req);

    @Select("SELECT MTL_PROPERTY,PROPERTY_NEeD,PROPERTY_SEQ,MTL_NO_NEED,SEQ_NO FROM SD_MTL_TYPE_DETAIL WHERE TYPE_NO = #{typeNo}" +
            "AND TYPE_NO2 = #{typeNo2} AND TYPE_NO3 = #{typeNo3} AND TYPE_NAME = #{typeName} AND TYPE_NAME2 = #{typeName2} AND TYPE_NAME3 = #{typeName3}")
    List<SdMtlTypeDetail> sdMtlTypeDetailList(SdMtlTypeDetail req);

    void insertSdMtlTypeDetail(@Param("req") SdMtlTypeDetail req, @Param("username") String username);

    @Select("SELECT MTL_SEQ mtlSeq,MTL_SPEC mtlSpec,SEQ_NO seqNo FROM SD_MTL_SPEC")
    List<Map<String, String>> getSpecs();

    @Select("SELECT MTL_SEQ mtlSeq,MTL_UNIT mtlUnit FROM SD_MTL_UNIT ORDER BY MTL_SEQ")
    List<Map<String, String>> getUnits();

    @Select("SELECT CAST(SEQ_NO AS DECIMAL(10,2))  seqNo,PRD_TYPE prdType FROM SD_MTL_PRD_TYPE ORDER BY SEQ_NO")
    List<Map<String, String>> getPrdTypes();

    @Select("SELECT DISTINCTROW MTL_PROPERTY mtlProperty,CAST(SEQ_NO AS DECIMAL(10,2)) seqNo FROM SD_MTL_PROPERTY ORDER BY SEQ_NO")
    List<Map<String, String>> getProperties();

    @Select("SELECT MTL_SEQ mtlSeq,MTL_SPEC mtlSpec FROM SD_MTL_PROPERTY_DETAIL WHERE MTL_PROPERTY = #{typeName} ")
    List<Map<String,String>> mtlPropertyRD(String typeName);

    @Insert("INSERT INTO SD_MTL_PROPERTY(MTL_PROPERTY,SYS_USER,SYS_DATE,SEQ_NO) VALUES(NULLIF(#{name},''),NULLIF(#{username},''),now(),NULLIF(#{code},''))")
    void insertSdProperty(@Param("code") String code, @Param("name") String name, @Param("username") String username);

    @Select("INSERT INTO SD_MTL_SPEC(MTL_SPEC,SYS_USER,SYS_DATE,MTL_SEQ,SEQ_NO) VALUES(NULLIF(#{name},''),NULLIF(#{username},''),now(),NULLIF(#{code},''),NULLIF(#{seqNo},''))")
    void insertSdSpec(@Param("code") String code, @Param("name") String name, @Param("seqNo") String seqNo,@Param("username") String username);

    @Insert("INSERT INTO SD_MTL_UNIT(MTL_UNIT,SYS_USER,SYS_DATE,MTL_SEQ) VALUES(NULLIF(#{name},''),#{username},now(),NULLIF(#{code},''))")
    void insertSdUnit(@Param("code") String code, @Param("name") String name, @Param("username") String username);

    @Insert("INSERT INTO SD_MTL_PRD_TYPE(PRD_TYPE,SYS_USER,SYS_DATE,SEQ_NO) VALUES(NULLIF(#{name},''),#{username},now(),NULLIF(#{code},''))")
    void insertSdPrd(@Param("code") String code, @Param("name") String name, @Param("username") String username);

    @Insert("INSERT INTO SD_MTL_PROPERTY_DETAIL(MTL_PROPERTY,MTL_SPEC,SYS_USER,SYS_DATE,MTL_SEQ) VALUES(NULLIF(#{mtlName},''),NULLIF(#{spec},''),#{userName},now(),NULLIF(#{seqNo},''))")
    void insertPropertyDetail(@Param("mtlName") String mtlName, @Param("seqNo") String seqNo, @Param("spec") String spec, @Param("userName") String userName);

    @Select("SELECT TYPE_NO,TYPE_NAME,LEVEL_CODE,PARENT_LEVEL FROM SD_CUST_TYPE")
    List<SdCustTypeDto> sdCustType();

    @Select("SELECT TYPE_NO,TYPE_NAME,LEVEL_CODE,PARENT_LEVEL FROM SD_VEN_TYPE WHERE COMPANY_ID = #{companyId}")
    List<SdCustTypeDto> sdVenType(@Param("companyId") String companyId);


    String getSdcustTypeByPL(String parentLevel);

    @Insert("INSERT INTO SD_CUST_TYPE(TYPE_NO,TYPE_NAME,LEVEL_CODE,PARENT_LEVEL) VALUES(#{req.typeNo},#{req.typeName},#{levelCode},NULLIF(#{parentLevel},''))")
    void insertSdCustType(@Param("req") SdCustTypeDto req, @Param("levelCode") String levelCode, @Param("parentLevel") String parentLevel);


    String getVenTypeByPL(String parentLevel);

    @Insert("INSERT INTO SD_VEN_TYPE(TYPE_NO,TYPE_NAME,LEVEL_CODE,PARENT_LEVEL,COMPANY_ID) VALUES(#{req.typeNo},#{req.typeName},#{venLevelCode},NULLIF(#{parentLevel},''),#{req.companyId})")
    void insertVen(@Param("req") SdCustTypeDto req, @Param("venLevelCode") String venLevelCode, @Param("parentLevel") String parentLevel);

    @Delete("DELETE FROM SD_CUST_TYPE WHERE TYPE_NO = #{typeNo}")
    void deleteSdCust(SdCustTypeDto req);

    @Delete("DELETE FROM SD_VEN_TYPE WHERE TYPE_NO = #{typeNo}")
    void deleteSdVen(SdCustTypeDto req);

    List<SdMtlNew> sdMtlNewList(SdMtlNewReq req);

    @Select("SELECT TYPE_NO FROM SD_MTL_TYPE_NEW WHERE TYPE_NAME = #{typeName}  LIMIT 1")
    String getTypeNoByName(String typeName);

    @Select("SELECT MAX(SUBSTR(MTL_NO,-5)) FROM SD_MTL_NEW WHERE SUBSTR(MTL_NO,1,4) = #{typeNo}")
    Integer getMaxByTypeNo(String typeNo);


    void insertSdMtlNew(@Param("req") SdMtlNew req, @Param("username") String username);

    void updateSdMtlNew(@Param("req") SdMtlNew req, @Param("username") String username);

    @Delete("DELETE FROM SD_MTL_NEW WHERE MTL_NO = #{mtlNo}")
    void deleteSdMtlNew(SdMtlNew req);

    void deleteByList3(@Param("request") PcOrderMtlOrderCreateRequest request, @Param("userName") String userName);

    void deleteSdBomMtl(String mtlNo);

    void deleteSdBomFitSub(@Param("mtlNo") String mtlNo);

    List<Vendor> selectByVenType(VenType venType);

    /**
     * 查询材料领料
     * @param request
     * @return
     */
    List<MaterialOutDto> getMaterialOutList(MaterialOutRequest request);

    void insertIntoKf(@Param("rep") PcOrderMaterToAllRep rep, @Param("username") String username);
}
